<?php
namespace app\admin\controller;
use think\Db;
use think\Request;
use think\Controller;
use app\admin\model\Pops;
use app\admin\model\Areas;
class Pop extends Common{
    public function _initialize()
    {
        parent::_initialize();
        $this->Pops = new Pops;
    }
    public function index($page=null,$key=null){
        if($this->request->isPost()){
            $page = (int)$page > 0 ? $page : 1;
            $map=[];
            if(!empty($key)){
                 $map['name'] = ['like',"%".$key."%"];
            }
            $lists = $this->Pops
                ->field("*,address as addr,area_id as country,city_id as city")
                ->order('area_id asc,name asc,city_id asc,create_time desc')
                ->where($map)
                ->paginate(array('list_rows'=>$this->pageSize,'page'=>$page))
                ->toArray();
            return $result = ['code'=>0,'msg'=>lang('No Data...'),'data'=>$lists['data'],'count'=>$lists['total'],'rel'=>1];
        }
    	return $this->fetch();
    }		
    public function edit($id=null){
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['name'])){
    			return ['code'=>0,'msg'=>lang('Please enter the name')];
    		}
    		// if(empty($data['address'])){
      //           return ['code'=>0,'msg'=>lang('Please input address')];
      //       }
    		if($id > 0){
    			$resutl = db('pops')->update($data);
    		}else{
    			$data['create_time'] = time();
    			$data['status'] = 1;
    			$resutl = db('pops')->insert($data);
    		}
    		if($resutl){
    			return ['code'=>1,'msg'=>lang('The data is saved successfully!'),'url'=>url('index')];
    		}
    		return ['code'=>1,'msg'=>lang('Data save failed!')];
    	}
        $row = [];
        if($id > 0){
            $row = db('pops')->where(['id'=>$id])->find();
        }
        $this->assign('areas',Areas::getAreas(0));
    	$this->assign('row', $row);
    	return $this->fetch('form');
    }
    //设置广告状态
    public function editState(){
        $id=input('post.id');
        $status=input('post.status');
        if(db('pops')->where('id='.$id)->update(['status'=>$status])!==false){
            return ['status'=>1,'msg'=>lang('Set up successfully.')];
        }else{
            return ['status'=>0,'msg'=>lang('Failed.')];
        }
    }
    public function sort(){
        $pops=db('pops');
        $data = input('post.');
        if($pops->update($data)!==false){
            return $result = ['msg' => lang('successful.'),'code'=>1];
        }else{
            return $result = ['code'=>0,'msg'=>lang('Failed.')];
        }
    }
    /*删除数据*/
    public function delete($id=null){
        if($id > 0 && $this->Pops->where(['id'=>$id])->delete()){
            return ['code'=>1,'msg'=>lang('Delete success!')];
        }
        return ['code'=>0,'msg'=>lang('Delete data failure')];
    }
    public function popList($pid=0){
        if(intval($pid) > 0){
            $pops = Pops::getPOPCitys($pid);
        }else{
            
            $pops = Pops::getPOPCountrys($pid);
        }
        if($pops){

            return ['code'=>1,'msg'=>lang('Set up successfully.'),'items'=>$pops];
        }else{
            return ['code'=>0,'msg'=>lang('Failed.')];
        }
    }
    public function popItems($city_id=0,$area_id=0){
        $items = db('pops')->where(['city_id'=>$city_id,'area_id'=>$area_id,'status'=>1])->select();
        if($items){
            return ['code'=>1,'msg'=>lang('Set up successfully.'),'items'=>$items];
        }else{
            return ['code'=>0,'msg'=>lang('Failed.')];
        }
    }
    public function view(){
        $Countrys = Pops::getPOPCountrys(0);
        $this->assign('countrys',$Countrys);
        return $this->fetch();
    }
    public function upload(){
        return $this->fetch();
    }
}